﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace SMSTester.Controls
{
    public partial class SchemaItem : PictureBox
    {

        public bool _Checked = false; 
        public bool Checked 
        {
            get { return _Checked; }
            set 
            {
                if (DesignMode)
                    return;
                if (this.Parent == null)
                    return;
                
                foreach (Control ctrl in this.Parent.Controls)
                { 
                    if (((SchemaItem)ctrl).Image != null)
                    if (ctrl.GetType() == typeof(SchemaItem))
                    {
                        ((SchemaItem)ctrl)._Checked = this.Tag == ((SchemaItem)ctrl).Tag;

                        if ((((SchemaItem)ctrl).InitialImage == null)
                            ||
                             (((SchemaItem)ctrl).InitialImage.Width != ((SchemaItem)ctrl).Image.Width)
                            ||
                             (((SchemaItem)ctrl).InitialImage.Height != ((SchemaItem)ctrl).Image.Height))
                            ((SchemaItem)ctrl).InitialImage = ((SchemaItem)ctrl).Image;

                        if (((SchemaItem)ctrl)._Checked)
                        {
                            ColorMatrix cmPicture = new ColorMatrix(new float[][]
                            {
                                new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
                                new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
                                new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
                                new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
                                new float[] {0.5f, 0.0f, 0.9f, 0.0f, 1.0f}
                            });

                            // Image attributes for setting the attributes of the picture
                            ImageAttributes iaPicture = new ImageAttributes();

                            Bitmap bmpPicture  = new Bitmap(((SchemaItem)ctrl).InitialImage);

                             // Set the new color matrix
                            iaPicture.SetColorMatrix(cmPicture);
                            // Set the Graphics object from the bitmap
                            Graphics gfxPicture = Graphics.FromImage(bmpPicture);
                            // New rectangle for the picture, same size as the original picture
                            Rectangle rctPicture = new Rectangle(0, 0, bmpPicture.Width, bmpPicture.Height);
                            // Draw the new image
                            gfxPicture.DrawImage(((SchemaItem)ctrl).InitialImage, rctPicture, 0, 0, bmpPicture.Width, bmpPicture.Height, GraphicsUnit.Pixel, iaPicture);
                            // Set the PictureBox to the new inverted colors bitmap
                            ((SchemaItem)ctrl).Image = bmpPicture;
                            
                        }
                        else
                        ((SchemaItem)ctrl).Image = ((SchemaItem)ctrl).InitialImage;
                    }
                }
            }
        }

        protected override void OnClick(EventArgs e)
        {
            Checked = true;
            base.OnClick(e);
        }
        
        public SchemaItem()
        {
            InitializeComponent();
        }
    }
}
